// managers/pages/my_approval/my_approval.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
      label: '待处理',
      list: []
    }, {
      label: '已处理',
      list: []
    }, ],
    isMusk: false, //遮罩
    active: 0, //选项卡
    content: 0,
  },


  getData() {
    wx.showLoading({
      title: '',
      icon: ''
    })
    var url = app.globalData.url + 'exam/getExamMain',
      that = this,
      tabList = this.data.tabList,
      content = 0;

    app.wxRequest(url, {
      memberId: app.globalData.memberId
      // memberId: 1
    }, res => {
      if (res.flag) {
        console.log(res);
        tabList[0].list = res.data.examMainList
        content = tabList[0].list.length;
        console.log(this.data.tabList)
        that.setData({
          tabList,
          content
        })
        that.getAlreadyExamMain()
      }
    })
  },
  // 获取已审批列表
  getAlreadyExamMain() {
    var url = app.globalData.url + 'exam/getAlreadyExamMain',
      that = this,
      tabList = this.data.tabList;

    app.wxRequest(url, {
      memberId: app.globalData.memberId
    }, res => {
      if (res.flag) {
        console.log(res);
        tabList[1].list = res.data.examMainList

        for (var i in tabList) {
          for (var j in tabList[i].list) {
            var date = new Date(tabList[i].list[j].submit_time.replace(/-/g, "/"));
            var time = "";
            if (date.getHours() > 12) {
              time = "下午";
            } else {
              time = "上午"
            }
            var afterTime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            var aTime = time + date.getHours() + ':' + date.getMinutes();
            tabList[i].list[j].submit_time = afterTime;
            tabList[i].list[j].submit_time = tabList[i].list[j].submit_time;
            tabList[i].list[j].time_detail = aTime;
          }

        }
        console.log(this.data.tabList)
        that.setData({
          tabList
        })
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    })

  },

  onChange(e) {
    console.log(e);
    this.setData({
      active: e.detail.index
    })
  },
  // 跳转到相应审批
  itemOpen(e) {
    console.log(e);
    var item = e.currentTarget.dataset.obj;

    if (this.data.active == 0) {
      item.detial = false;
    } else {
      item.detial = true;
      item.showBtn = true;
    }
    item.approval = true;
    item.stautsApproval = false;
    if (item.exam_type == 0) {
      item.noBtn = true;
      wx.navigateTo({
        url: '/approval/pages/leave_approval/leave_approval',
      })
    } else if (item.exam_type == 1) {
      wx.navigateTo({
        url: '/approval/pages/items_approval/items_approval',
      })
    } else if (item.exam_type == 2) {
      wx.navigateTo({
        url: '/approval/pages/depart_approval/depart_approval',
      })
    } else if (item.exam_type == 3) {
      item.noBtn = false;
      wx.navigateTo({
        url: '/approval/pages/leave_approval/leave_approval',
      })
    } else if (item.exam_type == 4) {
      wx.navigateTo({
        url: '/approval/pages/positive_apply/positive_apply',
      })
    } else if (item.exam_type == 5) {
      wx.navigateTo({
        url: '/approval/pages/return_approval/return_approval',
      })
    } else if (item.exam_type == 6) {
      wx.navigateTo({
        url: '/approval/pages/person_apply/person_apply',
      })
    }
    wx.setStorageSync('examInfo', JSON.stringify(item))


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})